<?php
namespace app\common\model;
use app\common\model\AdminLog;
use app\common\model\AdminGroup;
use think\Model;
/**
 * @Author: YeMiao
 * @Date:   2017-07-31 10:09:38
 * @Last Modified by:   YeMiao
 * @Last Modified time: 2017-09-12 09:48:00
 */ 
class AdminUser extends Model{

	protected $resultSetType = 'collection';

	// 获取用户
	public function find_user($str){
		try {
			$user = $this->where('user',$str)->find()->toArray();		
		} catch (\Exception $e) {
			return 0;
		}		
		return $user;
	}

	// 获取全部数据
	public function get_list(){
		$user = $this
			->alias('a')
			->field('a.*, b.name catename')
			->join('admin_group b','a.group = b.group')
			->select()
			->toArray();
		return $user;
	}

	// 添加用户
	public function add_user($data){
		$data['add_user'] = input('session.user');
		$data['password'] = md5($data['password']);
		try {
			$user = $this->save($data);	
		} catch (\Exception $e) {
			return 0;
		}		
		$log = new AdminLog();
		$log->add_log('添加用户,user:"'.$data['user'].'"');
		return $user;
	}
	// 添加头像
	public function add_head_img($data){		
		try {
			$head_img = $this->save(['head_img' => $data['head_img']],['id' => $data['id']]);		
		} catch (\Exception $e) {

			return $e;
		}
		if(input('session.user')){
			$user = input('session.user');
		}else{
			$user = input('cookie.user');
		}
		$log = new AdminLog();
		$log->add_log('为用户"'.$user.'"添加头像');	
		return $head_img;
	}
	// 添加用户信息
	public function add_user_content($data){		
		try {
			$user_content = $this->update($data);		
		} catch (\Exception $e) {
			return 0;
		}
		if(input('session.user')){
			$user = input('session.user');
		}else{
			$user = input('cookie.user');
		}
		$log = new AdminLog();
		$log->add_log('为用户"'.$user.'"添加信息');	
		return 1;
	}
	// 更改密码
	public function update_user_password($data){
		try {
			unset($data['repassword']);
			$data['password'] = md5($data['password']);
			$user_password = $this->update($data);		
		} catch (\Exception $e) {
			return 0;
		}			
		if(input('session.user')){
			$user = input('session.user');
		}else{
			$user = input('cookie.user');
		}
		$log = new AdminLog();
		$log->add_log('为用户"'.$user.'"修改密码');			
		return 1;
	}
	// 更新用户状态
	public function update_state_user($data){
		if($data['state'] == 0){
			$data['state'] = 1;
			$str = '激活';
		}else{
			$data['state'] = 0;
			$str = '冻结';
		}
		try {
			$user = $this->save(['state' => $data['state']],['id' => $data['id']]);
			$name = $this->field('user')->where('id',$data['id'])->find()->toArray();
		} catch (\Exception $e) {
			return 0;
		}		
		$log = new AdminLog();
		$log->add_log($str.'用户,user:'.$name['user']);		
		return $user;
	} 

	// 更改用户组
	public function update_group_user($data){
		$user = $this->field('user,group')->find($data['id'])->toArray();
		$group = new AdminGroup();
		$group_list = $group->get_list();
		foreach ($group_list as $key => $value) {
			if($value['group'] == $user['group']){
				$q = $value['name'];
			}elseif($value['group'] == $data['group']){
				$h = $value['name'];
			}
		}
		$log = new AdminLog();
		$log->add_log('改变'.$user['user'].'的用户组:"'.$q.'"为"'.$h.'"');
		$user = $this->save(['group' => $data['group']],['id' => $data['id']]);
		return $user;
	}
	
	// 删除用户
	public function del_user($data){
		$find = $this->get($data['id'])->toArray();
		$log = new AdminLog();
		$log->add_log('删除用户,id:'.$data['id'].'user:"'.$find['user'].'"');
		try {
			$user = $this->where('id',$data['id'])->delete();
		} catch (\Exception $e) {
			return 0;
		}				
		return $user;
	}
}

 ?>